

function ComDataTable(url, target,column_id) {
    this.$serverurl = url;
    this.$target = target;
    this.$columnid  = column_id;
    _tarObject = this;
}
ComDataTable.prototype.setRenders = function(renders){
    this.$renders = renders;
}
ComDataTable.prototype.initRenders = function(){
    //初始化columns的格式化列
    if(this.$renders){
        for(i=0;i<_tarObject.$columns.length;i++){
            if(this.$renders[_tarObject.$columns[i].data]){
                _tarObject.$columns[i].render = this.$renders[_tarObject.$columns[i].data];
            }
        }
    }
}
ComDataTable.prototype.rdraw = function(){
    this.$table.draw();
}
ComDataTable.prototype.initColumns = function(){

    //获取对应的columns配置信息
    this.$columns =  language.getColumns(this.$columnid);
    //组装table的header内容区域
    var header="<thead><tr>";
    for(i=0;i<this.$columns.length;i++){
        header +=  "<td>"+this.$columns[i].name+"</td>";
    }
    header += "</tr></thead>";

    this.$target.append(header);
}
ComDataTable.prototype.init = function(){
    this.initColumns();
    this.initRenders();
    TableTools.DEFAULTS.aButtons = ["copy", "xls"];
    this.$table = this.$target.DataTable({
        "processing": true,
        "serverSide": true,
        "ordering": false,
        "searching": false,
        //"sScrollX":true,
        "fnServerData": this.retrieveData,
        //"dom":  '<"top"i>rt<"bottom"flp> ',
        "dom": 'T<"clear">lfrtip',
        "columns": _tarObject.$columns,
        "oLanguage":language.getTabLanguage(),
        "tableTools": {
            "sSwfPath": "/assets/admin/js/plugins/dataTables/swf/copy_csv_xls_pdf.swf"
        }
    });
}

ComDataTable.prototype.retrieveData = function( sSource, aoData, fnCallback ) {
    //将客户名称加入参数数组
    aoData.push( { "name": "customerName", "value": $("#customerName").val() } );
    //console.log(aoData);
    $.ajax( {
        "type": "POST",
        "contentType": "application/json",
        "url": _tarObject.$serverurl,
        "dataType": "json",
        //"data":aoData, //以json格式传递
        "data": JSON.stringify(aoData), //以json格式传递
        "success": function(resp) {
            fnCallback(resp.res_data); //服务器端返回的对象的returnObject部分是要求的格式
        }
    });
}
/**
 * Created by sunkejun on 15/10/17.
 */
$.ajaxSetup({
    headers: {
        'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
    }
})

var ComdataUtil  = (function ($) {
    this.collectData = function(container){
        var data = {};
        $("#"+container+" input,textarea,select,img").each(function () {

            if($(this).attr('name')){
                if($(this)[0].tagName == "IMG"){
                    data[$(this).attr('name')]= $(this).attr('src');
                }
                else{
                    if(this.type == 'checkbox'){
                        data[$(this).attr('name')]= $(this).is(":checked")? 1:0;
                    }
                    else{
                        data[$(this).attr('name')]= $(this).val();
                    }
                }

            }
        });

        return data;
    }

    this.clear = function(container){
        $("#"+container+" input,textarea,select").each(function () {
            if($(this).attr('name')){
               $(this).val("");
            }
        });
    }
    this.loadContainer = function(container,obj){
        var key,value,tagName,type,arr;
        for(x in obj){
            key = x;
            if(obj[x] == null){
                continue;
            }
            value = obj[x]+"";

           // $("[name='"+key+"'],[name='"+key+"[]']").each(function(){
            $("#"+container+" [name='"+key+"'],[name='"+key+"[]']").each(function () {
                tagName = $(this)[0].tagName;
                type = $(this).attr('type');

                if(tagName=='INPUT'){
                    if(type=='radio'){
                        $(this).attr('checked',$(this).val()==value);
                    }else if(type=='checkbox'){
                        arr = value.split(',');
                        for(var i =0;i<arr.length;i++){
                            if($(this).val()==arr[i]){
                                $(this).attr('checked',true);
                                break;
                            }
                        }
                    }else{
                        $(this).val(value);
                    }
                }else if(tagName=='SELECT' || tagName=='TEXTAREA'){
                    $(this).val(value);
                }

            });
        }
    }

    this.ajaxpost = function(url,data,callback){

        $.ajax({
            url:  url,
            type:"POST",
            dataType:'JSON',
            data: data,
            success:function(response){
                if(response.code !=500){
                    openToastr.open(1,response.res_data);
                }
                if(callback){
                    callback(response);
                }

            }
        });
    }
    this.ajaxpostnormal = function(url,data,callback,type){

        $.ajax({
            url:  url,
            type:type?type:"POST",
            dataType:'JSON',
            data: data,
            success:function(response){
                if(callback){
                    callback(response);
                }

            }
        });
    }

    return this;
})(jQuery);

/**
 * Created by sunkejun on 2015/10/15.
 */
var openToastr  = (function ($) {
    //...
    var U = {};
    U.open = function(type,title,msg){
        toastr.options = {
            "closeButton": true,
            "debug": false,
            "progressBar": true,
            "preventDuplicates": false,
            "positionClass": "toast-top-right",
            "onclick": null,
            "showDuration": "400",
            "hideDuration": "1000",
            "timeOut": "3000",
            "extendedTimeOut": "1000",
            "showEasing": "swing",
            "hideEasing": "linear",
            "showMethod": "fadeIn",
            "hideMethod": "fadeOut"
        }
        //console.log(title,msg);
        var types = ["warning","success","info","error"];
        toastr[types[type>types.length?types.length-1:type]](title, "");
    }
    return U;
})(jQuery);